﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using YamlDotNet.Serialization;

namespace per.esmool.utils {

    public static class YamlKit {

        public static T Parse<T>(StreamReader yamlReader) where T : JToken {
            var deserializer = new DeserializerBuilder().Build();
            var yamlObject = deserializer.Deserialize(yamlReader);
            var serializer = new SerializerBuilder()
                .JsonCompatible()
                .Build();
            var json = serializer.Serialize(yamlObject);
            T result = JsonConvert.DeserializeObject<T>(json);
            return result;
        }

        public static T ParseFile<T>(string filepath) where T : JToken {
            using StreamReader reader = File.OpenText(filepath);
            return Parse<T>(reader);
        }
    }
}
